import global from '@/utils/global'
import http from '@/utils/request'

export function getList(searchForm) {
  return http({
    url: http.adornUrl(global.upms + '/account/page'),
    headers: { // application/json; charset=utf-8 //application/x-www-form-urlencoded; charset=utf-8
      'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    method: 'post',
    data: http.adornData({
      'accountName': searchForm.accountName === '' ? null : searchForm.accountName,
      'phone': searchForm.phone === '' ? null : searchForm.phone,
      'orgIds': searchForm.orgIds === '' ? null : searchForm.orgIds,
      'status': searchForm.status === '' ? null : searchForm.status,
      'begin': searchForm.beginDate === '' ? null : searchForm.beginDate,
      'end': searchForm.endDate === '' ? null : searchForm.endDate,
      'pageNum': searchForm.pageNum === '' ? null : searchForm.pageNum,
      'pageSize': searchForm.endDate === '' ? null : searchForm.pageSize
    }, true, 'form')
  })
}

export function getRoleByAccountId(id) {
  return http({
    url: http.adornUrl(global.upms + '/account/role/' + id),
    method: 'get'
  })
}

export function add(modelForm) {
  return http({
    url: http.adornUrl(global.upms + '/account'),
    method: 'post',
    data: http.adornData({
      'accountId': modelForm.accountId,
      'accountName': modelForm.accountName,
      'truename': modelForm.truename,
      'password': modelForm.password,
      'phone': modelForm.phone,
      'sex': modelForm.sex,
      'age': modelForm.age,
      'organizationId': modelForm.organizationId,
      'status': modelForm.status
    })
  })
}

export function update(modelForm) {
  return http({
    url: http.adornUrl(global.upms + '/account/' + modelForm.accountId),
    method: 'patch',
    data: http.adornData({
      'accountId': modelForm.accountId,
      'accountName': modelForm.accountName,
      'truename': modelForm.truename,
      'phone': modelForm.phone,
      'sex': modelForm.sex,
      'age': modelForm.age,
      'organizationId': modelForm.organizationId,
      'status': modelForm.status
    })
  })
}

export function deleteOne(id) {
  return http({
    url: http.adornUrl(global.upms + '/account/batch/' + id),
    method: 'delete'
  })
}

export function deleteBatch(ids) {
  return http({
    url: http.adornUrl(global.upms + '/account/delete/batches'),
    headers: { // application/json; charset=utf-8 //application/x-www-form-urlencoded; charset=utf-8
      'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    method: 'post',
    data: http.adornData({
      'ids': ids
    }, true, 'form')
  })
}

export function saveRoleByAccountId(accountId, roleIds) {
  return http({
    url: http.adornUrl(global.upms + '/account/accountRole'),
    headers: { // application/json; charset=utf-8 //application/x-www-form-urlencoded; charset=utf-8
      'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    method: 'post',
    data: http.adornData({
      'accountId': accountId,
      'roleIds': roleIds
    }, true, 'form')
  })
}

export function updatePwdByAccountId(modelForm) {
  return http({
    url: http.adornUrl(global.upms + '/account/updatePwd'),
    headers: { // application/json; charset=utf-8 //application/x-www-form-urlencoded; charset=utf-8
      'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    method: 'post',
    data: http.adornData({
      'accountId': modelForm.accountId,
      'oldPwd': modelForm.oldPwd,
      'newPwd': modelForm.newPwd
    }, true, 'form')
  })
}
